#include "VContainer.h"
#include "VMain.h"

VContainer::VContainer(void) :
    VNRWidget(),
    _children(new VWidgetArray())
{
}

VContainer::VContainer(size_t size) :
    VNRWidget(),
    _children(new VWidgetArray(size))
{
}

VContainer::~VContainer(void)
{
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        delete *it;
    }

    delete this->_children;
}

VWidget* VContainer::at(size_t pos) const
{
    return this->_children->at(pos);
}

VWidget* VContainer::operator[](size_t pos) const
{
    return this->_children->at(pos);
}

size_t VContainer::size() const
{
    return this->_children->size();
}

bool VContainer::insert(VWidget* widget)
{
    if (find(widget))   // 若存在此元素
    {
        return false;
    }

    this->_children->push_back(widget);
    widget->setParent(this);

    sort(_children->begin(), _children->end(), &VWidgetZOrderCompare);

    return true;
}

CLASS_TYPE VContainer::classType() const
{
    return VContainer::_clstype;
}

bool VContainer::render() const
{
    if (!VNRWidget::render())
    {
        return false;
    }

    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        VMain::resetViewport();
        glPushMatrix();
        (*it)->render();
        glPopMatrix();
    }

    return true;
}

void VContainer::animate()
{
    VWidget::animate();

    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        (*it)->animate();
    }
}

bool VContainer::mouseDownEvent(int x, int y)
{
    bool ret = false;
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        ret = (*it)->mouseDownEvent(x, y) || ret;
    }

    return ret;
}

bool VContainer::mouseUpEvent(int x, int y)
{
    bool ret = false;
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        ret = (*it)->mouseUpEvent(x, y) || ret;
    }

    return ret;
}

bool VContainer::mouseMoveEvent(int x, int y, bool mousestat)
{
    if (!_mousetrace)
    {
        return false;
    }

    bool ret = false;
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        ret = (*it)->mouseMoveEvent(x, y, mousestat) || ret;
    }

    return ret;
}

bool VContainer::find(const VWidget* widget)
{
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        if (*it == widget)
        {
            return true;
        }
    }

    return false;
}

bool VContainer::remove(VWidget* widget)
{
    VWidgetArray::iterator it = _children->begin();

    for (; it != _children->end(); ++it)
    {
        if (*it == widget)
        {
            widget->setParent(NULL);
            _children->erase(it);
            return true;
        }
    }

    return false;
}

int VWidgetZOrderCompare(VWidget* w1, VWidget* w2)
{
    return (w1->zOrder() < w2->zOrder());
}
